#include "../include/userCenterDao.h"
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include "../include/redis_pool.h"
#include "../include/md5.h"
userCenterDao::userCenterDao()
{

}

bool userCenterDao::register_user(const ::message::reqRegisterUser *request, ::message::resRegisterUser *response)
{
    //从mysql池子里获取一个mysql连接
    
    auto mysql_con=userCenterDaoPool.get_con();
    mysql_con->_con->setAutoCommit(false);  //相当于开启事务

    //判断邮箱是否已经被注册
    
    auto pre_stm=mysql_con->_con->prepareStatement("select email from user where email=?");
    pre_stm->setString(1,request->email());
    
    auto res=pre_stm->executeQuery();
    
    if(res->next()){
        //如果有数据，那么表示已经创建了该用户，那么无法再度创建
        response->set_result(false);
        response->set_info("邮箱已经被注册！！");
        mysql_con->_con->commit();
        delete res;
        delete pre_stm;
    }else{
        //无论正确与否都要释放掉
        
        delete res;
        delete pre_stm;
        //判断邮箱对应的验证码是否正确---从redis_pool总读取
        MyRedis * one=redis_pool::get_instance()->get_one_redis();
        std::string true_authCode=one->get(request->email().c_str());

        // redis_pool::get_instance()->give_back_one_redis(one); //不着急归还
        if(true_authCode!=request->authcode()){
            response->set_result(false);
            response->set_info("验证码错误！！");
            mysql_con->_con->commit();
        }else{
            //进行注册写入
            pre_stm=mysql_con->_con->prepareStatement("insert into user(email,passwd,user_name) values(?,?,?)");
            pre_stm->setString(1,request->email());
            //密码采用MD5加密
            std::string md5_passwd=MD5::get_md5(request->passwd());
            std::cout<<"测试加密后的密码："<<md5_passwd<<std::endl;
            pre_stm->setString(2,md5_passwd);
            pre_stm->setString(3,request->username());
            int affected_rows=pre_stm->executeUpdate();
            if(affected_rows!=1){
                //为什么写入失败
                response->set_result(false);
                response->set_info("插入失败，不应该会出现这个错误！！--可能同时注册了，但是事务保证了一致性");
            }else{

                response->set_result(true);
                response->set_info("注册成功！！");
                //删除对应的验证码
                one->del_key(request->email().c_str()); //不在乎返回结果

                std::cout<<"成功完成注册"<<std::endl;
            }
            delete pre_stm;
            mysql_con->_con->commit();
            

        }

        redis_pool::get_instance()->give_back_one_redis(one);
        

    }
    
    

    //回收资源

    mysql_con->_con->setAutoCommit(true);
    userCenterDaoPool.return_con(mysql_con);

    return true;
}

bool userCenterDao::reset_passwd(const ::message::reqResetPasswd *request, ::message::resResetPasswd *response)
{
    auto mysql_con=userCenterDaoPool.get_con();
    //开启事务，去从数据库中查找是否有email对应的用户数据
    mysql_con->_con->setAutoCommit(false);
    sql::PreparedStatement *pre_stm=mysql_con->_con->prepareStatement("select email from user where email=?");
    pre_stm->setString(1,request->email());
    sql::ResultSet *res=pre_stm->executeQuery();
    if(res->next()){
        //有数据才往下走--验证验证码的有效性与正确性
        delete res;
        delete pre_stm;
        MyRedis *one=redis_pool::get_instance()->get_one_redis();
        std::string true_authCode=one->get(request->email().c_str());
        
        if(true_authCode==request->authcode()){
            //相等，那进行一个重置操作
            std::string new_passwd=MD5::get_md5(request->new_passwd());
            pre_stm=mysql_con->_con->prepareStatement("update user set passwd=? where email=?");
            pre_stm->setString(1,new_passwd);
            pre_stm->setString(2,request->email());
            int affected_rows=pre_stm->executeUpdate();
            //清除原来的验证码
            one->del_key(request->email().c_str());
            response->set_result(true);
            response->set_info("密码重置成功！！");
            //这里依旧做一个删除
            delete pre_stm; 
        }else{
            response->set_result(false);
            response->set_info("验证码错误，请重试！！");

        }
        redis_pool::get_instance()->give_back_one_redis(one);

    }else{
        delete pre_stm;
        response->set_result(false);
        response->set_info("该邮箱并未注册用户！！");
    }

    //回收资源
    mysql_con->_con->setAutoCommit(true);
    mysql_con->_con->commit();
    userCenterDaoPool.return_con(mysql_con);
    return false;
}


bool userCenterDao::login(const ::message::reqLogin *request, ::message::resLogin *response)
{
    //获取mysql连接
    auto mysql_con=userCenterDaoPool.get_con();
    //开启事务
    mysql_con->_con->setAutoCommit(false);

    auto pre_stm=mysql_con->_con->prepareStatement("select passwd,user_name from user where email=?");
    pre_stm->setString(1,request->email());
    auto res=pre_stm->executeQuery();
    if(res->next()){
        std::string data_passwd=res->getString("passwd");
        std::string md5_passwd=MD5::get_md5(request->passwd());
        if(md5_passwd==data_passwd){
            response->set_result(true);
            response->set_info(res->getString("user_name"));
        }else{
            response->set_result(false);
            response->set_info("密码错误！！");
        }
        

    }else{
        response->set_result(false);
        response->set_info("用户不存在！！");
    }
    delete res;
    delete pre_stm;
    mysql_con->_con->commit();
    mysql_con->_con->setAutoCommit(false);
    
    userCenterDaoPool.return_con(mysql_con);

    return false;
}
